<template>
	<view class="address-container">
		<!-- 地址列表 -->
		<view class="address-list">
			<radio-group @change="radioChange">
				<view class="address-item" v-for="item in addressList" :key="item.id" @click="selectAddress(item.id)">
					<view class="address-info">
						<view class="address-top">
							<text class="name">{{item.userName}}</text>
							<text class="phone">{{item.phone}}</text>
							<text class="default-tag" v-if="item.defaultAddress === 1">默认</text>
						</view>
						<view class="address-detail">{{item.address}}</view>
					</view>
					<radio :value="item.id" :checked="selectedAddressId === item.id" color="#ff6700" />
				</view>
			</radio-group>
		</view>

		<!-- 确认按钮 -->
		<view class="confirm-btn">
			<button @click="confirmAddress">确认地址</button>
		</view>
	</view>
</template>

<script>
	import {
		request
	} from '@/utils/request.js';

	export default {
		data() {
			return {
				addressList: [],
				selectedAddressId: null
			}
		},
		onLoad() {
			this.loadAddressList();
		},
		methods: {
			async loadAddressList() {
				try {
					const res = await request({
						url: 'address/queryAddressByUserId',
						method: 'GET'
					});

					if (res.code === 200) {
						this.addressList = res.data;
						// 初始化选中默认地址（仅用于显示，不影响选择）
						const defaultAddr = this.addressList.find(item => item.defaultAddress === 1);
						if (defaultAddr) {
							this.selectedAddressId = defaultAddr.id;
						}
					}
				} catch (error) {
					console.error('加载地址失败:', error);
				}
			},

			selectAddress(addressId) {
				this.selectedAddressId = addressId;
			},

			radioChange(e) {
				this.selectedAddressId = e.detail.value;
			},

			confirmAddress() {
				if (!this.selectedAddressId) {
					uni.showToast({
						title: '请选择地址',
						icon: 'none'
					});
					return;
				}

				const selectedAddress = this.addressList.find(item => item.id === this.selectedAddressId);

				// 获取前一页实例并更新数据
				const pages = getCurrentPages();
				const prevPage = pages[pages.length - 2]; // confirmOrder 页面实例
				if (prevPage && prevPage.$vm) {
					prevPage.$vm.address = {
						addressId: selectedAddress.id,
						username: selectedAddress.userName,
						phone: selectedAddress.phone,
						address: selectedAddress.address
					};
					prevPage.$vm.addressId = selectedAddress.id;
					uni.navigateBack(); // 返回上一页，保持页面栈
				}
			}
		},
	}
</script>

<style lang="scss" scoped>
	.address-container {
		padding: 20rpx;
		height: 100vh;
		background-color: #f5f5f5;
	}

	.address-list {
		margin-bottom: 120rpx;
	}

	.address-item {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 30rpx;
		margin-bottom: 20rpx;
		background-color: #fff;
		border-radius: 10rpx;
	}

	.address-info {
		flex: 1;
		margin-right: 20rpx;
	}

	.address-top {
		display: flex;
		align-items: center;
		margin-bottom: 10rpx;
	}

	.name {
		font-size: 28rpx;
		font-weight: bold;
		margin-right: 20rpx;
	}

	.phone {
		font-size: 28rpx;
		color: #666;
		margin-right: 20rpx;
	}

	.default-tag {
		font-size: 24rpx;
		color: #fff;
		background-color: #ff6700;
		padding: 4rpx 10rpx;
		border-radius: 6rpx;
	}

	.address-detail {
		font-size: 26rpx;
		color: #333;
	}

	.confirm-btn {
		position: fixed;
		bottom: 20rpx;
		left: 20rpx;
		right: 20rpx;

		button {
			background-color: #ff6700;
			color: #fff;
			border-radius: 50rpx;

			&[disabled] {
				background-color: #ccc;
			}
		}
	}
</style>